package com.zhongfl.crm.lixiao.config;

import com.heptagram.lixiao.api.LiXiaoConfigStorage;
import com.heptagram.lixiao.api.LiXiaoService;
import com.heptagram.lixiao.api.impl.LiXiaoServiceApacheHttpClientImpl;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

/**
 * @author wang.yuchuan
 * @date 2018/6/22
 */
@Configuration
@ConditionalOnClass(LiXiaoConfig.class)
public class LiXiaoConfig {

    @Resource
    private LiXiaoConfigProperties liXiaoConfigProperties;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Bean
    @ConditionalOnMissingBean
    public LiXiaoService liXiaoService() {
        LiXiaoServiceApacheHttpClientImpl liXiaoServiceApacheHttpClient = new LiXiaoServiceApacheHttpClientImpl();
        liXiaoServiceApacheHttpClient.setLiXiaoConfigStorage(liXiaoConfigStorage());
        return liXiaoServiceApacheHttpClient;
    }

    @Bean
    @ConditionalOnMissingBean
    public LiXiaoConfigStorage liXiaoConfigStorage() {
        LiXiaoRedisConfigStorage liXiaoRedisConfigStorage = new LiXiaoRedisConfigStorage(redisTemplate);
        liXiaoRedisConfigStorage.setLogin(liXiaoConfigProperties.getLogin());
        liXiaoRedisConfigStorage.setPassword(liXiaoConfigProperties.getPassword());
        return liXiaoRedisConfigStorage;
    }
}
